package com.woniuxy.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.woniuxy.controller.info.UserInfo;
import com.woniuxy.model.Account;
import com.woniuxy.service.AccountService;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

@Service
public class LoginService implements UserDetailsService {
    @Resource
    private AccountService accountService;

    @Override
    public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
        QueryWrapper<Account> queryWrapper = new QueryWrapper<Account>();
        queryWrapper.eq("username",username);
        Account account = accountService.getOne(queryWrapper);
        if(account==null){
            throw new UsernameNotFoundException("账号不存在");
        }
        UserInfo userInfo = new UserInfo();
        userInfo.setId(account.getAId());
        userInfo.setAccountName(account.getUsername());
        userInfo.setAccountPwd(account.getPassword());
        return userInfo;
    }
}
